import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue'),
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/Register.vue'),
    },
    {
      path: '/',
      name: 'dashboard',
      component: () => import('../views/Dashboard.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/health-data',
      name: 'health-data',
      component: () => import('../views/HealthData.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/reminders',
      name: 'reminders',
      component: () => import('../views/Reminders.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/profile',
      name: 'profile',
      component: () => import('../views/Profile.vue'),
      meta: { requiresAuth: true }
    },
  {
    path: '/health-report',
    name: 'HealthReport',
    component: () => import('../views/HealthReport.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/reminder-settings',
    name: 'ReminderSettings',
    component: () => import('../views/ReminderSettings.vue'),
    meta: { requiresAuth: true }
  }
],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  if (to.meta.requiresAuth && !token) {
    next('/login')
  } else if (to.path === '/login' && token) {
    next('/')
  } else {
    next()
  }
})

export default router
